package violet.p2.ui;

import violet.p2.bean.Customer;
import violet.p2.service.CustomerList;
import violet.p2.util.CMUtility;

/**
 * CustomerView 为主模块 负责菜单的显示和处理用户操作
 *
 * @author shkstart
 * @date 2022/5/5 - 21:11
 */

/*思路：
 * 1.先创建Customer类 把类中的姓名、性别、年龄、电话、邮箱
 * 以private形式封装上。然后方法set和get、还有构造器的正在创建对象的初始化
 * 2.再创建CustomerList类 把类中的Customer数组 客户数量 接着就是增删改查客户
 * 然后还有得到单个客户数量和全部客户数量 的返回值方法 （重点就是增删改查的内容比较难一点）
 * 3.接着就是CustomerView 来调用创建的类
 * */
public class CustomerView {
    CustomerList customerList = new CustomerList(8);
    public CustomerView(){
        Customer customer = new Customer("王涛", '男', 23, "15412341234", "wt@qq.com");
        customerList.addCustomer(customer);
    }

    public void enterMainMenu(){
        boolean isFlag = true;
        while(isFlag){
            System.out.println("-----------------客户信息管理软件-----------------");
            System.out.println("                   1 添 加 客 户");
            System.out.println("                   2 删 除 客 户");
            System.out.println("                   3 修 改 客 户");
            System.out.println("                   4 客 户 列 表");
            System.out.println("                   5 退       出\n");
            System.out.print("                   请选择(1-5)：");

            char menu = CMUtility.readMenuSelection();

            switch(menu) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    deleteCustomer();
                    break;
                case '3':
                    modifyCustomer();
                    break;
                case '4' :
                    listAllCustomers();
                    break;
                case '5' :
                    System.out.print("确认是否退出(Y/N):");
                    char isExit = CMUtility.readConfirmSelection();
                    if(isExit == 'Y') {
                        isFlag = false;
                    }

            }
        }

    }

    private void modifyCustomer() {
        System.out.println("---------------------修改客户---------------------");
        int number;
        Customer cust;
        for(;;) {
            System.out.print("请选择待修改客户编号(-1退出)：");

             number = CMUtility.readInt();
            if(number == -1 ){
                return;
            }

            cust = customerList.getCustomer(number - 1);
            if(cust == null) {
                System.out.println("无法找到指定客户！");
            }else {
                break;
            }
        }
        System.out.print("姓名(" + cust.getName() + "):");
        String name = CMUtility.readString(10, cust.getName());
        System.out.print("性别(" + cust.getGender() + "):");
        char gender = CMUtility.readChar(cust.getGender());
        System.out.print("年龄(" + cust.getAge() + "):");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("电话(" + cust.getPhone() + "):");
        String phone = CMUtility.readString(14, cust.getPhone());
        System.out.print("邮箱(" + cust.getEmail() + "):");
        String email = CMUtility.readString(30, cust.getEmail());
        cust.setAge(age);
        cust.setEmail(email);
        cust.setGender(gender);
        cust.setName(name);
        cust.setPhone(phone);
        boolean isRepalaced = customerList.replaceCustomer(number - 1,cust);
        if(isRepalaced) {
            System.out.println("---------------------修改完成---------------------");
        }else {
            System.out.println("---------------------修改失败--------------------");
        }

    }

    private void listAllCustomers() {
        System.out.println("---------------------------客户列表---------------------------");
        int total = customerList.getTotal();
        if(total == 0 ) {
            System.out.println("没有客户记录！");
        }else {
            System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
            Customer[] custs = customerList.getAllCustomer();
            for(int i = 0; i < total; i++ ) {// 一开始我是 i < total -1 但其实不是 反而会使我显示少一个客户
                System.out.println((i + 1) + "\t" + custs[i].getName() +
                        "\t" + custs[i].getGender() + "\t"
                        + custs[i].getAge() + "\t" + custs[i].getPhone() + "\t"
                        + custs[i].getEmail());
            }
        }

    }

    public void addNewCustomer(){
        System.out.println("---------------------添加客户---------------------");//忘记换行了，不好看
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别：");
        char gender = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(13);
        System.out.print("邮箱：");
        String email = CMUtility.readString(30);

        Customer cust = new Customer(name,gender,age,phone,email);

        boolean isAddOk = customerList.addCustomer(cust);
        if (isAddOk) {
            System.out.println("---------------------添加完成---------------------");
        }else {
            System.out.println("---------------------添加失败---------------------");
        }

    }


    public void deleteCustomer() {
        System.out.println("---------------------删除客户---------------------");
        int number;
        for(;;){
            System.out.print("请选择待删除客户编号(-1退出)：");
             number = CMUtility.readInt();
            if(number == -1 ){
                return;
            }

            Customer customer = customerList.getCustomer(number - 1);
            if (customer == null) {
                System.out.println();
            }else {
                break;
            }
        }

        System.out.print("确认是否删除(Y/N)：");
        char isDelete = CMUtility.readConfirmSelection();
        if(isDelete == 'Y'){
            boolean deleteSuccess = customerList.deleteCustomer(number - 1);
            if(deleteSuccess){
                System.out.println("---------------------删除完成---------------------");
            }else {
                System.out.println("---------------------删除失败---------------------");
            }
            }else {
            return;
        }

    }

    public static void main(String[] args) {
            CustomerView customerView = new CustomerView();
            customerView.enterMainMenu();

    }
}
